﻿/* ==============================================================================
* 功能描述：LoginMsgHandler
* 创 建 者：青金石
* 创建日期：2020/5/29 18:05:19
* ==============================================================================*/

using System;
using TCPServer.script.db;
using TCPServer.script.net;
using TCPServer.script.proto;

public partial class MsgHandler
{
    //处理注册协议
    public static void MsgRegister(ClientState state, MsgBase msgBase)
    {
        MsgRegister mr = (MsgRegister)msgBase;
        //注册
        if (DBManager.Register(mr.id, mr.pw))
        {
            Console.WriteLine("register succ");
            DBManager.CreatePlayer(mr.id);
            //构建Player
            Player player = new Player(state);
            player.id = mr.id;
            player.state = state;
            PlayerData data = DBManager.GetPlayerData(mr.id);
            player.data = data;
            PlayerManager.AddPlayer(mr.id, player);
            state.player = player;
            mr.result = 0;
        }
        else
        {
            Console.WriteLine("register fail");
            mr.result = 1;
        }
        NetManager.Send(state, mr);
    }

    //处理登录协议
    public static void MsgLogin(ClientState state, MsgBase msgBase)
    {
        MsgLogin msgLogin = (MsgLogin)msgBase;
        //密码错误
        if (!DBManager.CheckPassword(msgLogin.id, msgLogin.pw))
        {
            msgLogin.result = 1;
            NetManager.Send(state, msgLogin);
            return;
        }
        //不允许同个ip再次登录同个账号
        if (state.player != null)
        {
            msgLogin.result = 1;
            NetManager.Send(state, msgLogin);
            return;
        }
        //踢人下线,避免多人登录同个账号
        if (PlayerManager.isOnline(msgLogin.id))
        {
            //发送踢人协议
            Player kickPlayer = PlayerManager.GetPlayer(msgLogin.id);
            MsgKick kick = new MsgKick();
            kick.reason = 0;
            kickPlayer.Send(kick);
            // 断开连接
            NetManager.Close(kickPlayer.state);
            return;
        }

        //获取玩家数据
        PlayerData playerData = DBManager.GetPlayerData(msgLogin.id);

        //获取玩家昵称
        string name = playerData.name;
        if (playerData != null)
        {
            //构建Player
            Player player = new Player(state);
            player.id = msgLogin.id;
            player.state = state;
            player.data = playerData;
            PlayerManager.AddPlayer(msgLogin.id, player);
            state.player = player;
            //返回登录协议
            //玩家注册后，未取名就退出游戏，则跳到取名界面
            if (string.IsNullOrEmpty(name))
            {
                msgLogin.result = 2;
            }
            else
            {
                msgLogin.result = 0;
            }
            player.Send(msgLogin);
        }
        else
        {
            msgLogin.result = 1;
            NetManager.Send(state, msgLogin);
            return;
        }
    }

    // 收到MsgName协议
    public static void MsgName(ClientState state, MsgBase msgBase)
    {
        MsgName msg = (MsgName)msgBase;
        //存在非法字符
        if (!DBManager.IsSafeString(msg.name))
        {
            Console.WriteLine("name is  not safe");
            msg.result = 2;
            state.player.Send(msg);
            return;
        }
        //如果昵称存在
        if (DBManager.isNameExist(msg.name))
        {
            msg.result = 1;
            state.player.Send(msg);
            return;
        }
        // 修改玩家昵称
        if (!DBManager.UpdatePlayerName(state.player.id, msg.name))
        {
            msg.result = 3;
            state.player.Send(msg);
            return;
        }
        //修改玩家数据,返回协议
        PlayerData data = DBManager.GetPlayerData(state.player.id);
        data.name = msg.name;
        if (DBManager.UpdatePlayerData(state.player.id, data))
        {
            msg.result = 0;
            state.player.data = data;
            state.player.Send(msg);
        }
        else
        {
            msg.result = 3;
            state.player.Send(msg);
        }
    }
}